package com.lemon.smartsafetynew.ui.activity.me;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.net.services.MeServices;
import com.lemon.smartsafetynew.ui.adapter.me.MeCompanyListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 * Created by Andrew on 2017/8/30.
 */
public class MeCompanyListActivity extends SwipeBackActivity
        implements RecyclerTClickListener<MeCompanyVo> {

    @Bind(R.id.lineRight)
    LinearLayout lineRight;
    @Bind(R.id.txtSearch)
    TextView txtSearch;
    @Bind(R.id.txtClearn)
    TextView txtClearn;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private MeCompanyListAdapter mAdapter;
    private List<MeCompanyVo> mDataList = new ArrayList<>();

    @Override
    protected void initTitle() {
        this.setBarTitle("FAE功能页");
    }

    @Override
    protected void callFinish() {
        MeCompanyListActivity.this.finish();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_me_company_list;
    }

    @Override
    protected void initViews() {
        super.initViews();

        lineRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getNetData();
            }
        });

        recyclerView();
    }

    private void recyclerView() {
        mAdapter = new MeCompanyListAdapter(mDataList, this,false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MeCompanyListActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(mAdapter);
    }


    private void getNetData() {
        User user = AccountUtil.getUser();
        String company_id = "";
        if (user != null) {
            company_id = user.getCompanyId();
        }
        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", company_id);
        params.put("sKeyword", editText.getText().toString().trim());
        MeServices.init().getCompanyList(params);
    }


    @Subscribe
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_COMPANY_LIST.equals(event.getTag())) { //获取企业列表
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow() && recyclerView != null) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    List<MeCompanyVo> list = (List<MeCompanyVo>) event.getObj();
                    if (mAdapter != null) {
                        mDataList.clear();
                        mAdapter.notifyDataSetChanged();
                        mAdapter.addItems(list);
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }
            }
        }
    }

    @Override
    public void onItemClick(MeCompanyVo data, int position) {

        CompanyInfo mCompanyInfo = new CompanyInfo();
        mCompanyInfo.setCompany_code(StringUtils.nullValue(data.getCompany_code()));
        mCompanyInfo.setCompany_name(StringUtils.nullValue(data.getCompany_name()));
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
        UIHelper.showSafeDataCollectionCheckItemBuildingList(MeCompanyListActivity.this, bundle);

    }

}
